#include "PAKReader.h"
#include "..\utils\Log.h"

namespace ds {

PAKReader::PAKReader() {
}


PAKReader::~PAKReader() {
	close();
}

bool PAKReader::open(const char* fileName) {
	m_File = fopen(fileName,"rb");
	m_EOF = false;
	return m_File != 0;
}

void PAKReader::close() {
	if ( m_File != 0 ) {
		fclose(m_File);
		m_File = 0;
	}
}

int PAKReader::readInt() {
	int v;
	size_t cnt = fread(&v,sizeof(int),1,m_File);
	if ( cnt != 1 ) {
		m_EOF = true;
	}
	return v;
}

uint32 PAKReader::readUInt() {
	uint32 v;
	size_t cnt = fread(&v,sizeof(uint32),1,m_File);
	if ( cnt != 1 ) {
		m_EOF = true;
	}
	return v;
}

bool PAKReader::readBool() {
	int c = readInt();
	return c == 0 ? false : true;
}

std::string PAKReader::readString() {
	uint32 length = readUInt();
	std::string ret;
	for ( uint32 i = 0; i < length; ++i ) {
		ret += readChar();
	}
	return ret;
}

char PAKReader::readChar() {
	char c;
	size_t cnt = fread(&c,sizeof(char),1,m_File);
	if ( cnt != 1 ) {
		m_EOF = true;
	}
	return c;
}

uchar PAKReader::readUChar() {
	uchar c;
	size_t cnt = fread(&c,sizeof(uchar),1,m_File);
	if ( cnt != 1 ) {
		m_EOF = true;
	}
	return c;
}

float PAKReader::readFloat() {
	float v;
	size_t cnt = fread(&v,sizeof(float),1,m_File);
	if ( cnt != 1 ) {
		m_EOF = true;
	}
	return v;
}

Vec2 PAKReader::readVec2() {    
	Vec2 vec;
	vec.x = readFloat();
	vec.y = readFloat();
	return vec;
}

Vec3 PAKReader::readVec3() {
	Vec3 vec;
	vec.x = readFloat();
	vec.y = readFloat();
	vec.z = readFloat();
	return vec;
}

Color PAKReader::readColor() {
	Color color;
	color.r = readFloat();
	color.g = readFloat();
	color.b = readFloat();
	color.a = readFloat();
	return color;
}


}
